package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.system.pojo.HandleProjectCategory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.util.Assert;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService {
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        return dictDataMapper.selectDictDataByType(dictType);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 通过字典ID删除字典数据信息
     *
     * @param dictCode 字典数据ID
     * @return 结果
     */
    @Override
    public int deleteDictDataById(Long dictCode) {
        return dictDataMapper.deleteDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据
     *
     * @param ids 需要删除的数据
     * @return 结果
     */
    @Override
    public int deleteDictDataByIds(String ids) {
        return dictDataMapper.deleteDictDataByIds(Convert.toStrArray(ids));
    }

    /**
     * 新增保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData dictData) {
        return dictDataMapper.insertDictData(dictData);
    }

    /**
     * 修改保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData dictData) {
        return dictDataMapper.updateDictData(dictData);
    }

    @Override
    public List<HandleProjectCategory> getAllProjectCategory(String dictType) {
        List<SysDictData> sysDictData = this.selectDictDataByType(dictType);
        if (CollectionUtils.isNotEmpty(sysDictData)) {
            List<SysDictData> categoryFirstList = sysDictData.stream().filter(e -> e.getDictValue().equals("无")).collect(Collectors.toList());
            Assert.notEmpty(categoryFirstList, "处理项目一级分类不存在");
            return categoryFirstList.parallelStream().map(first -> {
                HandleProjectCategory category = new HandleProjectCategory();
                BeanUtils.copyProperties(first, category);

                List<SysDictData> categorySecondList = dictDataMapper.selectDictDataByValue(first.getDictCode().toString());
                if (CollectionUtils.isEmpty(categorySecondList)) {
                    return category;
                }
                category.setCategorySecondList(categorySecondList.parallelStream().map(second -> {
                    HandleProjectCategory.HandleProjectCategorySecond categorySecond = new HandleProjectCategory.HandleProjectCategorySecond();
                    BeanUtils.copyProperties(second, categorySecond);

                    List<SysDictData> categoryThirdList = dictDataMapper.selectDictDataByValue(second.getDictCode().toString());
                    if (CollectionUtils.isEmpty(categoryThirdList)) {
                        return categorySecond;
                    }
                    categorySecond.setCategoryThirdList(categoryThirdList.parallelStream().map(third -> {
                        HandleProjectCategory.HandleProjectCategorySecond.HandleProjectCategoryThird categoryThird = new HandleProjectCategory.HandleProjectCategorySecond.HandleProjectCategoryThird();
                        BeanUtils.copyProperties(third, categoryThird);
                        return categoryThird;
                    }).collect(Collectors.toList()));
                    return categorySecond;
                }).collect(Collectors.toList()));
                return category;
            }).collect(Collectors.toList());
        }
        return null;
    }
}
